//------------------------------------------------------------------------------
// Fichiers d'ent�te utilis�s
//------------------------------------------------------------------------------
#include "Orientation.hpp"

//------------------------------------------------------------------------------
// Définition de constantes
//------------------------------------------------------------------------------

namespace ImageAnalysis
{

		//----------------------------------------------------------------------
	    // Permet de convertir un angle en une direction approxim�. L'angle doit
	    // �re compris entre 0 et 360 degr�
	    //----------------------------------------------------------------------
	    Orientation::TDirection Orientation::ConvertAngleToDirection( float angle )
	    {
	        if( (angle >= 0.0f) && angle <= 22.5f )
			{
				return Orientation::DIRECTION_0;
			}
			else if( angle <= 67.5f )
			{
				return Orientation::DIRECTION_1;
			}
			else if( angle <= 112.5f )
			{
				return Orientation::DIRECTION_2;
			}
			else if( angle <= 157.5f )
			{
				return Orientation::DIRECTION_3;
			}
			else if( angle <= 202.5f )
			{
				return Orientation::DIRECTION_4;
			}	
			else if( angle <= 247.5f )
			{
				return Orientation::DIRECTION_5;
			}
			else if( angle <= 292.5f )
			{
				return Orientation::DIRECTION_6;
			}
			else if( angle <= 337.5f )
			{
				return Orientation::DIRECTION_7;
			}
			else // On se ram�e au cas ou l'angle vaut 0
			{
				return Orientation::DIRECTION_0;
			}
	    }

	    float Orientation::ConvertDirectionToAngle( TDirection direction )
	    {
		switch (direction)
		{
			case DIRECTION_0:
			return 0.f;

			case DIRECTION_1:
			return 45.f;

			case DIRECTION_2:
			return 90.f;	

			case DIRECTION_3:
			return 135.f;

			case DIRECTION_4:
			return 180.f;

			case DIRECTION_5:
			return 225.f;

			case DIRECTION_6:
			return 270.f;

			case DIRECTION_7:
			return 315.f;

		}
	    }

	//----------------------------------------------------------------------
	//
	//----------------------------------------------------------------------
        Orientation::TDirection Orientation::ConvertDirectionToTDirection( int direction )
	{
		switch (direction)
		{
			case 0: 
			return DIRECTION_0;

			case 1:
			return DIRECTION_1;

			case 2:
			return DIRECTION_2;	

			case 3:
			return DIRECTION_3;

			case 4:
			return DIRECTION_4;

			case 5:
			return DIRECTION_5;

			case 6:
			return DIRECTION_6;

			case 7:
			return DIRECTION_7;
		}
	}
	
	    void Orientation::GetPossibleDirection( Orientation::TDirection normalDirection, Orientation::TDirection* possibleDirection )
	    {
	        if( normalDirection == Orientation::DIRECTION_0 )
			{
				possibleDirection[0] = Orientation::DIRECTION_1;
				possibleDirection[1] = Orientation::DIRECTION_2;
				possibleDirection[2] = Orientation::DIRECTION_3;
			}
			else if( normalDirection == Orientation::DIRECTION_1 )
			{
				possibleDirection[0] = Orientation::DIRECTION_2;
				possibleDirection[1] = Orientation::DIRECTION_3;
				possibleDirection[2] = Orientation::DIRECTION_4;
			}
			else if( normalDirection == Orientation::DIRECTION_2 )
			{
				possibleDirection[0] = Orientation::DIRECTION_3;
				possibleDirection[1] = Orientation::DIRECTION_4;
				possibleDirection[2] = Orientation::DIRECTION_5;
			}
			else if( normalDirection == Orientation::DIRECTION_3 )
			{
				possibleDirection[0] = Orientation::DIRECTION_4;
				possibleDirection[1] = Orientation::DIRECTION_5;
				possibleDirection[2] = Orientation::DIRECTION_6;
			}
			else if( normalDirection == Orientation::DIRECTION_4 )
			{
				possibleDirection[0] = Orientation::DIRECTION_5;
				possibleDirection[1] = Orientation::DIRECTION_6;
				possibleDirection[2] = Orientation::DIRECTION_7;
			}
			else if( normalDirection == Orientation::DIRECTION_5 )
			{
				possibleDirection[0] = Orientation::DIRECTION_6;
				possibleDirection[1] = Orientation::DIRECTION_7;
				possibleDirection[2] = Orientation::DIRECTION_0;
			}
			else if( normalDirection == Orientation::DIRECTION_6 )
			{
				possibleDirection[0] = Orientation::DIRECTION_0;
				possibleDirection[1] = Orientation::DIRECTION_1;
				possibleDirection[2] = Orientation::DIRECTION_7;
			}
			else if( normalDirection == Orientation::DIRECTION_7 )
			{
				possibleDirection[0] = Orientation::DIRECTION_0;
				possibleDirection[1] = Orientation::DIRECTION_1;
				possibleDirection[2] = Orientation::DIRECTION_2;
			}
	    }

		Orientation::TDirection Orientation::GetOppositeDirection( Orientation::TDirection direction )
		{
			if( direction == Orientation::DIRECTION_0 )
			{
				return Orientation::DIRECTION_4;
			}
			else if( direction == Orientation::DIRECTION_1 )
			{
				return Orientation::DIRECTION_5;
			}
			else if( direction == Orientation::DIRECTION_2 )
			{
				return Orientation::DIRECTION_6;
			}
			else if( direction == Orientation::DIRECTION_3 )
			{
				return Orientation::DIRECTION_7;
			}
			else if( direction == Orientation::DIRECTION_4 )
			{
				return Orientation::DIRECTION_0;
			}
			else if( direction == Orientation::DIRECTION_5 )
			{
				return Orientation::DIRECTION_1;
			}
			else if( direction == Orientation::DIRECTION_6 )
			{
				return Orientation::DIRECTION_2;
			}
			else //if( direction == Orientation::DIRECTION_7 )
			{
				return Orientation::DIRECTION_3;
			}
		}
	
		Orientation::TDirection Orientation::GetOrthogonalDirection( Orientation::TDirection direction )
		{
			if( direction == Orientation::DIRECTION_0 )
			{
				return Orientation::DIRECTION_2;
			}
			else if( direction == Orientation::DIRECTION_1 )
			{
				return Orientation::DIRECTION_3;
			}
			else if( direction == Orientation::DIRECTION_2 )
			{
				return Orientation::DIRECTION_4;
			}
			else if( direction == Orientation::DIRECTION_3 )
			{
				return Orientation::DIRECTION_5;
			}
			else if( direction == Orientation::DIRECTION_4 )
			{
				return Orientation::DIRECTION_6;
			}
			else if( direction == Orientation::DIRECTION_5 )
			{
				return Orientation::DIRECTION_7;
			}
			else if( direction == Orientation::DIRECTION_6 )
			{
				return Orientation::DIRECTION_0;
			}
			else //if( direction == Orientation::DIRECTION_7 )
			{
				return Orientation::DIRECTION_1;
			}
		}
}
